﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LTreesLibrary.Trees;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Utils;
using Microsoft.Xna.Framework;

namespace PloobsEngine.Modelo
{
    public class TreeModel : IModelo
    {
        public TreeModel(String profileName,String trunktextureName, String LeaftextureName)
        {
            this.trunktextureName = trunktextureName;
            this.LeaftextureName = LeaftextureName;
            this.profileName = profileName;
            LoadModelo();
        }

        public TreeModel(String profileName)
        {         
            this.profileName = profileName;
            LoadModelo();
        }


        BatchInformation[][] batchInformationS = null;
        VertexBuffer[] vertexBufferS = null;
        IndexBuffer[] indexBufferS = null;

        String trunktextureName;
        String LeaftextureName;
        String profileName;
        SimpleTree tree;

        public SimpleTree Tree
        {
            get { return tree; }
            set { tree = value; }
        }

        #region IModelo Members

        public void LoadModelo()
        {
            TreeProfile t = EngineStuff.CustomContentManager.GetAsset<TreeProfile>(profileName);
            if (!String.IsNullOrEmpty(LeaftextureName))
            {
                t.LeafTexture = EngineStuff.CustomContentManager.GetAsset<Texture2D>(LeaftextureName);
            }
            if (!String.IsNullOrEmpty(trunktextureName))
            {
                t.TrunkTexture = EngineStuff.CustomContentManager.GetAsset<Texture2D>(trunktextureName);
            }
            tree = t.GenerateSimpleTree(StaticRandom.RandomInstance);
            

            batchInformationS = new BatchInformation[2][];
            vertexBufferS = new VertexBuffer[2];
            indexBufferS = new IndexBuffer[2];
            indexBufferS[0] = tree.TrunkMesh.IndexBuffer;
            vertexBufferS[0] = tree.TrunkMesh.VertexBuffer;
            BatchInformation bi0 = new BatchInformation(0, tree.TrunkMesh.NumberOfVertices, tree.TrunkMesh.NumberOfTriangles, 0, 0, tree.TrunkMesh.Vdeclaration,TreeVertex.SizeInBytes);
            batchInformationS[0] = new BatchInformation[1];
            batchInformationS[0][0] = bi0; 

            indexBufferS[1] = tree.LeafCloud.Ibuffer;
            vertexBufferS[1] = tree.LeafCloud.Vbuffer;
            BatchInformation bi1 = new BatchInformation(0, tree.LeafCloud.Numleaves * 4, tree.LeafCloud.Numleaves * 2, 0, 0, tree.LeafCloud.Vdeclaration, LeafVertex.SizeInBytes);
            batchInformationS[1] = new BatchInformation[1];
            batchInformationS[1][0] = bi1; 

        }

        public void Serialize(System.Xml.XmlTextWriter textWriter)
        {
            throw new NotImplementedException();
        }

        public void DeSerialize(System.Xml.XmlNode node, EngineStuff engine)
        {
            throw new NotImplementedException();
        }

        public object getModelTag()
        {
            throw new NotImplementedException();
        }

        public Microsoft.Xna.Framework.Graphics.ModelMesh[] getModelMeshes()
        {
            throw new NotImplementedException();
        }

        Matrix[] m = new Matrix[] { Matrix.Identity };
        public Microsoft.Xna.Framework.Matrix[] getBonesTransformation()
        {
            return m;
        }

        public Microsoft.Xna.Framework.Graphics.Texture2D getTexture(TextureType textureType)
        {
            throw new NotImplementedException();
        }

        public Microsoft.Xna.Framework.BoundingSphere GetBoudingSphere()
        {
            return tree.TrunkMesh.BoundingSphere;
        }

        public int GetParentBoneIndex(int meshNumber)
        {
            return 0;
        }

        public int MeshNumber
        {
            get { return 2; }
        }

        public Microsoft.Xna.Framework.Graphics.VertexBuffer GetVertexBuffer(int meshNumber)
        {

            return vertexBufferS[meshNumber];
        }

        public Microsoft.Xna.Framework.Graphics.IndexBuffer GetIndexBuffer(int meshNumber)
        {
            return indexBufferS[meshNumber];
        }

        public BatchInformation[] GetBatchInformation(int meshNumber)
        {
            return batchInformationS[meshNumber];
        }

        public string Name
        {
            get
            {
                return profileName;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
